﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.BusinessLogic;

using Umbraco.Courier.Core;
using Umbraco.Courier.ItemProviders;
using Umbraco.Courier.Core.Enums;

namespace DeleteUnpublishedDocuments
{
//hook into the standard applicationbase for event subscriptions
public class EventSubscriber : ApplicationBase
{
    public EventSubscriber()
    {
        //get the DocumentItemProvider
        DocumentItemProvider.Instance().Extracted += new EventHandler<ItemEventArgs>(DataResolver_Extracted);
    }

    void DataResolver_Extracted(object sender, ItemEventArgs e)
    {
        //this is Courier Item, not a native umbraco document
        Document d = e.Item as Document;
        //the current provider used 
        ItemProvider provider = sender as ItemProvider;


        //if the document is not published, we queue the event "DeleteDocument" in the DBtransactionComplete queue
        //this will then be executed after the DB transaction is done.
        if(!d.Published)
            provider.ExecutionContext.QueueEvent("DeleteDocument", d.ItemId, null, EventManagerSystemQueues.DBTransactionComplete);
        }
    }
}